/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bionic.ce.commands.bankacc;

import com.bionic.ce.businesslogic.BankAccService;
import com.bionic.ce.commands.ICommand;
import com.bionic.ce.manager.Config;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author MAXIM
 */
public class CommandUpdateBankAcc implements ICommand {

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String page = null;
        Integer bankAccid = Integer.parseInt(request.getParameter("id"));
        Integer accNum = null;
        String currency = request.getParameter("currency");
        String bankName = request.getParameter("bankName");
        String swift = request.getParameter("swift");
        String correspondentBank = request.getParameter("correspondentBank");
        Integer userId = Integer.parseInt(request.getParameter("userId"));
        try {
            accNum = Integer.parseInt(request.getParameter("accNum"));
            BankAccService.updateCurrentBankacc(bankAccid, accNum, currency, bankName, swift, correspondentBank, userId);
            (new CommandGoToBankAccountInfo()).execute(request, response);
            page = Config.getInstance().getProperty(Config.GOTOBANKACCAOUNTSINFO);
        } catch (NumberFormatException ex) {
            request.setAttribute("currencyShortName", currency);
            request.setAttribute("bankName", bankName);
            request.setAttribute("swift", swift);
            request.setAttribute("correspondentBank", correspondentBank);
            request.setAttribute("bankaccregistererror", "Please provide correct bank account number");
            page = Config.getInstance().getProperty(Config.GOTOADDNEWBANKACC);
        } catch (IllegalStateException ex) {
            request.setAttribute("accNum", accNum);
            request.setAttribute("currencyShortName", currency);
            request.setAttribute("bankName", bankName);
            request.setAttribute("swift", swift);
            request.setAttribute("correspondentBank", correspondentBank);
            request.setAttribute("bankaccregistererror", "Not all required fields are filled");
            page = Config.getInstance().getProperty(Config.GOTOADDNEWBANKACC);
        }
        return page;
    }
}